#pragma once
#include<iostream>
#include<string>
#include<functional>
#include"Reactor.hpp"
#include"InetAddr.hpp"
class Connection;
class Reactor;
using handler_t = std::function<void(Connection*)>;
//封装任何一种连接
class Connection
{
public:
    Connection(int fd)
    {
        _fd = fd;
    }
    void RegisterHandler(handler_t recever,handler_t sender,handler_t excepter)
    {
        _handler_recever = recever;
        _handler_sender = sender;
        _handler_excepter = excepter;
    }
    void SetEvent(uint32_t event)
    {
        _event = event;
    }
    int Sockfd()
    {
        return _fd;
    }
    uint32_t Event()
    {
        return _event;
    }
    void SetReactor(Reactor* R)
    {
        _R = R;
    }
    void SetType(int type)
    {
        _type = type;
    }
    int Type()
    {
        return _type;
    }
    void SetAddr(InetAddr addr)
    {
        _addr = addr;
    }
    InetAddr Addr()
    {
        return _addr;
    }
    void SetInbufffer(std::string& str)
    {
        _inbuffer += str;
    }
    std::string& GetInbuffer()
    {
        return _inbuffer;
    }
    void SetOutbuffer(std::string& str)
    {
        _outbuffer += str;
    }
    std::string& GetOutbuffer()
    {
        return _outbuffer;
    }
    //丢弃已经发送的报文
    void DiscardOutbuffer(int n)
    {
        _outbuffer.erase(0,n);
    }
    void Close()
    {
        if(_fd >= 0)
        {
            ::close(_fd);
        }
    }
    ~Connection()
    {}

private:
    int _fd;
    int _type;
    InetAddr _addr;
    //所关心的事件
    uint32_t _event;
    //缓冲区
    std::string _inbuffer;
    std::string _outbuffer;
public: 
    //回掉函数
    handler_t _handler_recever;
    handler_t _handler_sender;
    handler_t _handler_excepter;
public:
    Reactor* _R;

};